/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author Danang
 */
public class Message {
    // Jenis field yang ada
    public static final int All_FIELD = 0;
    public static final int FROM_FIELD = 1;
    public static final int TO_FIELD = 2;
    public static final int SUBJECT_FIELD = 3;
    public static final int DATE_FIELD = 4;
    public static final int BODY_FIELD = 5;
    
    // id untuk semua message
    private static AtomicInteger id=new AtomicInteger(0);
    // atribut id dari message
    private int messageID;
    // atribut string date
    private String date="";
    // atribut string to
    private String to="";
    // atribut string from
    private String from="";
    // atribut string subject
    private String subject="";
    // atribut string body
    private String body="";
    
    private int dateIdx=0;
    private int toIdx=0;
    private int fromIdx=0;
    private int subjectIdx=0;
    private int bodyIdx=0;
    
    public Message() {
        messageID = getID();
        incrementID();
    }
    
    // Prosedur untuk menambahkan ID message secara statik dan atomik
    public void incrementID() {
        id.incrementAndGet();
    }
    
    // getter dari ID message
    public int getID() {
        return id.get();
    }
    
    // fungsi statik untuk mengembalikan nama field
    public static String getFieldName(int messageType) {
        switch (messageType) {
            case Message.All_FIELD: {
                return "all";
            }
            case Message.BODY_FIELD: {
                return "body";
            }
            case Message.DATE_FIELD: {
                return "date";
            }
            case Message.FROM_FIELD: {
                return "from";
            }
            case Message.SUBJECT_FIELD: {
                return "subject";
            }
            case Message.TO_FIELD: {
                return "to";
            }
            default:
                break;
        }
        return "";
    }
    
    // mengembalikan integer dari string field
    public static int getFieldType(String field) {
        int res = 0;
        if (field.equalsIgnoreCase("body"))
            res = Message.BODY_FIELD;
        else if (field.equalsIgnoreCase("to"))
            res = Message.TO_FIELD;
        else if (field.equalsIgnoreCase("from"))
            res = Message.FROM_FIELD;
        else if (field.equalsIgnoreCase("subject"))
            res = Message.SUBJECT_FIELD;
        else if (field.equalsIgnoreCase("date"))
            res = Message.DATE_FIELD;
        return res;
    }

    /**
     * @param date the date to set
     */
    public void setDate(String date) {
        this.date = date.toLowerCase();
    }

    /**
     * @param to the to to set
     */
    public void setTo(String to) {
        this.to = to.toLowerCase();
    }

    /**
     * @param from the from to set
     */
    public void setFrom(String from) {
        this.from = from.toLowerCase();
    }

    /**
     * @param subject the subject to set
     */
    public void setSubject(String subject) {
        this.subject = subject.toLowerCase();
    }

    /**
     * @param body the body to set
     */
    public void setBody(String body) {
        this.body = body.toLowerCase();
    }

    /**
     * @return the date
     */
    public String getDate() {
        return date;
    }

    /**
     * @return the to
     */
    public String getTo() {
        return to;
    }

    /**
     * @return the from
     */
    public String getFrom() {
        return from;
    }

    /**
     * @return the subject
     */
    public String getSubject() {
        return subject;
    }

    /**
     * @return the body
     */
    public String getBody() {
        return body;
    }
    
    // prosedur untuk menambahkan string teks ke body
    public void addBody(String str) {
        body = body.concat(str.toLowerCase());
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Date: ").append(date).append("\n");
        sb.append("From: ").append(from).append("\n");
        sb.append("To: ").append(to).append("\n");
        sb.append("Subject: ").append(subject).append("\n");
        sb.append("Body: ").append("\n").append(body).append("\n");
        sb.append("Idx Date ").append(dateIdx).append("\n");
        sb.append("Idx From ").append(fromIdx).append("\n");
        sb.append("Idx To ").append(toIdx).append("\n");
        sb.append("Idx Subject ").append(subjectIdx).append("\n");
        sb.append("Idx Body ").append(bodyIdx).append("\n");
        return sb.toString();
    }

    /**
     * @return the messageID
     */
    public int getMessageID() {
        return messageID;
    }

    /**
     * @return the dateIdx
     */
    public int getDateIdx() {
        return dateIdx;
    }

    /**
     * @param dateIdx the dateIdx to set
     */
    public void setDateIdx(int dateIdx) {
        this.dateIdx = dateIdx;
    }

    /**
     * @return the toIdx
     */
    public int getToIdx() {
        return toIdx;
    }

    /**
     * @param toIdx the toIdx to set
     */
    public void setToIdx(int toIdx) {
        this.toIdx = toIdx;
    }

    /**
     * @return the fromIdx
     */
    public int getFromIdx() {
        return fromIdx;
    }

    /**
     * @param fromIdx the fromIdx to set
     */
    public void setFromIdx(int fromIdx) {
        this.fromIdx = fromIdx;
    }

    /**
     * @return the subjectIdx
     */
    public int getSubjectIdx() {
        return subjectIdx;
    }

    /**
     * @param subjectIdx the subjectIdx to set
     */
    public void setSubjectIdx(int subjectIdx) {
        this.subjectIdx = subjectIdx;
    }

    /**
     * @return the bodyIdx
     */
    public int getBodyIdx() {
        return bodyIdx;
    }

    /**
     * @param bodyIdx the bodyIdx to set
     */
    public void setBodyIdx(int bodyIdx) {
        this.bodyIdx = bodyIdx;
    }
    
    public boolean isDateEmpty() {
        return date.isEmpty();
    }
    
    public boolean isFromEmpty() {
        return from.isEmpty();
    }
    
    public boolean isToEmpty() {
        return to.isEmpty();
    }
    
    public boolean isSubjectEmpty() {
        return subject.isEmpty();
    }
    
    public boolean isBodyEmpty() {
        return body.isEmpty();
    }
}


